/*
 * Pratama Nur Wijaya (c) 2013 
 * 
 * Project       : Sound Of Indonesia
 * Filename      : Scene_Splash.java
 * Creation Date : Mar 30, 2013 time : 9:48:42 PM
 *
 */

package id.pratama.soundofindonesia.scene;

import id.pratama.soundofindonesia.IManageableScene;
import id.pratama.soundofindonesia.SceneManager;
import id.pratama.soundofindonesia.Utils;
import id.pratama.soundofindonesia.config.IApps_Data;
import id.pratama.soundofindonesia.config.IApps_Define;
import id.pratama.soundofindonesia.data.IGraphic_Data;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.graphics.Typeface;

public class Scene_Splash extends Scene implements IManageableScene, IApps_Data, IApps_Define, IGraphic_Data
{
	private BaseGameActivity	activity;
	private SceneManager		sceneManager;
	private Font				myFont;
	private Text				teksSplash;
	private final float			TIMER	= 3;

	private BitmapTextureAtlas	texMenuBg;
	private ITextureRegion		regMenuBg;
	private Sprite				sprMenuBg;

	public Scene_Splash(BaseGameActivity activity)
	{
		this.activity = activity;
		sceneManager = SceneManager.getInstance();
		loadResources();
		applyResources();
		registerUpdateHandler();
	}

	@Override
	public void loadResources()
	{
		myFont = FontFactory.create(activity.getFontManager(), activity.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), Utils.getRatio(50));

		if (texMenuBg == null)
			texMenuBg = new BitmapTextureAtlas(activity.getTextureManager(), MENU_UTAMA_BG_TEX_WIDTH,
					MENU_UTAMA_BG_TEX_HEIGHT, Utils.getTextureOptions());

		regMenuBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBg, activity, MENU_UTAMA_BG, 0, 0);

		texMenuBg.load();
		myFont.load();
	}

	@Override
	public void applyResources()
	{
		// setBackground(new Background(Color.WHITE));

		if (sprMenuBg == null)
			sprMenuBg = new Sprite(0, 0, RES_WIDTH, RES_HEIGHT, regMenuBg, activity.getVertexBufferObjectManager());

		teksSplash = new Text(0, 0, myFont, "Sound of Indonesia",
				activity.getVertexBufferObjectManager());

		attachChild(sprMenuBg);

		teksSplash.setPosition(RES_WIDTH / 2 - teksSplash.getWidth() / 2, RES_HEIGHT / 2 - teksSplash.getHeight() / 2);

		sprMenuBg.attachChild(teksSplash);

	}

	@Override
	public void registerUpdateHandler()
	{
		this.registerUpdateHandler(new TimerHandler(TIMER, new ITimerCallback()
		{
			@Override
			public void onTimePassed(TimerHandler pTimerHandler)
			{
				Scene_Splash.this.unregisterUpdateHandler(pTimerHandler);
				sceneManager.setScene(SceneManager.SCENE_MENU_UTAMA);
			}
		}));
	}

	@Override
	public void unloadResources()
	{

	}

}
